package com.bs.service;

import com.bs.domain.Deploy;
import com.bs.domain.DeployArchive;
import com.bs.repository.DeployArchiveRepository;
import com.bs.repository.DeployRepository;
import com.bs.service.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * Created by wenguangjia on 2017/5/31.
 */
@Service
@Transactional
public class ArchiveTimerService {
    @Autowired
    private DeployRepository deployRepository;
    @Autowired
    private DeployArchiveRepository deployArchiveRepository;
    private Logger log= LoggerFactory.getLogger(ArchiveTimerService.class);

//    @Scheduled(fixedRate = 120000)//每个月最后一天归档 //归档只保留最近三个月的，5月在新的履历表，34月在归档表
    @Scheduled(cron = "0 0 0 1 * ?")//每个月最后一天归档 //归档只保留最近三个月的，5月在新的履历表，34月在归档表
    public void archive(){
        log.info("开始归档==================="+new Date());
        deployArchive();//deploy归档
        deploy2DeployArchive();//deployArchive归档
        log.info("归档完成=====================");
    }

    public void deploy2DeployArchive(){
        Timestamp timestamp= DateUtil.getBeforeDay(DateUtil.MONTH,1);
        List<Deploy> deploys=deployRepository.findBysubmitDateLessThan(timestamp);
        log.info("deploy表有"+deploys.size()+"条结果要归档");

        for(Deploy deploy:deploys){
            DeployArchive deployArchive=new DeployArchive();
            deployArchive.setId(deploy.getId());
            deployArchive.setTitle(deploy.getTitle());
            deployArchive.setDescription(deploy.getDescription());
            deployArchive.setModule(deploy.getModule());
            deployArchive.setType(deploy.getType());
            deployArchive.setLocation(deploy.getLocation());
            deployArchive.setCreateUser(deploy.getCreateUser());
            deployArchive.setSubmitDate(deploy.getSubmitDate());
            deployArchive.setChipInfo(deploy.getChipInfo());
            deployArchive.setMacAddress(deploy.getMacAddress());
            deployArchive.setState(deploy.getState());
            deployArchive.setPriority(deploy.getPriority());
            deployArchive.setFinishDate(deploy.getFinishDate());
            deployArchive.setExplanation(deploy.getExplanation());
            deployArchive.setPart(deploy.getPart());
            deployArchiveRepository.save(deployArchive);
            deployRepository.delete(deploy);
        }
    }
    public void deployArchive(){
        Timestamp timestamp= DateUtil.getBeforeDay(DateUtil.MONTH,2);
        List<DeployArchive> deployArchives = deployArchiveRepository.findBysubmitDateLessThan(timestamp);
        log.info("deployArchive表有"+deployArchives.size()+"条结果要归档");
        for(DeployArchive deployArchive:deployArchives){
            deployArchiveRepository.delete(deployArchive);
        }
    }

}
